package com.sixbro.shiro.session.exception;

import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 自定义异常处理类
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/17 15:35
 */
public class ExceptionResolver implements HandlerExceptionResolver {
    private static final Logger _logger = LoggerFactory.getLogger(ExceptionResolver.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        _logger.info("********  自定义异常处理类 "+ ExceptionResolver.class.getName() +" *********");

        //如果是shiro无权操作，因为shiro 在操作auno等一部分不进行转发至无权限url
        ex.printStackTrace();
        if (ex instanceof UnauthorizedException) {
            return new ModelAndView("unauth");
        }else if (ex instanceof UnknownAccountException){
            return new ModelAndView("login","msg","用户名错误");
        }else if (ex instanceof IncorrectCredentialsException){
            return new ModelAndView("login","msg","密码错误");
        }else if (ex instanceof LockedAccountException){
            return new ModelAndView("unauth","msg","账号已被锁定,请联系管理员！");
        }else if (ex instanceof DisabledAccountException){
            return new ModelAndView("unauth","msg","账号已被警用,请联系管理员！");
        }else if (ex instanceof AuthenticationException){
            return new ModelAndView("login","msg","用户认证异常");
        }else {
            return new ModelAndView("unauth","msg",ex.getMessage());
        }
    }
}
